<script>
    $(window).scroll(
    function()
    {
        scrollTop = $(window).scrollTop();
        docHeight = getDocHeight();
        winHeight = getWinHeight();
        scrolltop20 = Math.round((getDocHeight() - getWinHeight())*0.9);
        result = getDocHeight() - getWinHeight();
		
        console.log('Top Scroll: '+ scrollTop +' || Win Height: '+ winHeight +' || Doc Height: '+ docHeight+' || scrolltop20: '+scrolltop20+' || result :'+ result );
	
        if($(window).scrollTop() >= scrolltop20)
        {
            if(killScroll == false) 
            {
                killScroll = true;
				
                $('#loading').show();
                $('#loadding-text').show();			
                $('#loading').html('<img src="images/loading.gif" alt="" width="150" height="13" />');
				
                if($('.countReply:last').data('ctype') == 'reply_activity')
                {
                    lastReply('replyActivity', $('.countReply:last').data('activityid'), $('.countReply:last').data('numreply'));  
                }				
                setTimeout(function(){killScroll = false},500);	
            }
        }
    }
); 

    $(function () 
    {
        $('#link_top_page a').live('click',
        function()
        {
            $('body,html').animate({
                scrollTop: 0
            }, 800);
            return false;
        }
    );
    }
);


    function addReplyComment()
    {
        var fromData = '';
        if($('#reply_comment_details').val() == '')
        {
            $('#reply_comment_error').popup('open')
            setTimeout(
            function()
            {
                $('#reply_comment_error').popup('close')
                $('#reply_comment_details').focus();
            },1000
        );
        }
        else if($('#reply_comment_name').val() == '')
        {
            $('#reply_name_error').popup('open')
            setTimeout(
            function()
            {
                $('#reply_name_error').popup('close')
                $('#reply_comment_name').focus();
            },1000
        );
        }
        else
        {
            fromData =$('#from_reply_activity').serialize();		  			
		
            $.post('validate/addReplyActivity.php',fromData,
            function(data)
            {	
                if(data == 1)
                {
                    $('#reply_success').popup('open')
                    setTimeout(
                    function()
                    {
                        $('#reply_success').popup('close');
                        $('#reply_comment_details').val('');
                        $('#reply_comment_name').val('');
                        window.location.reload();
                    },1000
                );	
                }
                else
                {
                    alert('ล้มเหลว!');	
                }
            }
        );
        }
	
    }
</script> 
<!-- Start of Product Detail page -->
<div data-role="page" id="activityDetail">
    <div data-role="header">
        <h1>รายละเอียดกิจกรรม</h1>
        <a rel="external" id="link_web" href="?q=activity" data-icon="back" data-iconpos="right">กลับ</a>        
    </div><!-- /header -->

    <div data-role="content">
        <?php
        $res = $db->select_query('SELECT name FROM activity WHERE id = ' . $db->antihack($_GET['activityId']) . ' ');
        $getNews = $db->fetch($res);
        ?>    

        <ul data-role="listview">
            <li data-role="list-divider" id="listDivider_step">
                <a rel="external" href="?q=home">หน้าแรก</a>&nbsp;&nbsp;>>&nbsp;&nbsp;<a rel="external" href="?q=activity">กิจกรรม</a>&nbsp;&nbsp;>>&nbsp;&nbsp;<a ><?php echo $getNews['name']; ?></a>
            </li>         
        </ul>    
        <div id="activity_detail" style="margin-top: 30px;">
            <?php            
            $res = $db->select_query('SELECT activity_detail FROM activity WHERE id = ' . $db->antihack($_GET['activityId']) . ' ');
            $detail = $db->fetch($res);
            echo $detail['activity_detail'];
            
            ?>  
        </div>
        <div data-role="popup" id="reply_comment_error" data-theme="e">
            <p>-- กรุณากรอก "ข้อความ" ที่ต้องการ --<p>
        </div>

        <div data-role="popup" id="reply_name_error" data-theme="e">
            <p>-- กรุณากรอก "ชื่อ" ที่ต้องการ --<p>
        </div>  

        <div data-role="popup" id="reply_success" data-theme="e">
            <p>-- ตอบกระทู้สำเร็จ --<p>
        </div>     

        <div style="clear:both;">&nbsp;</div>
        <div id="reply_comment">
            <form method="post" id="from_reply_activity">
                <input name="activity_id" type="hidden" value="<?php echo $_GET['activityId']; ?>" />
                ข้อความ
                <textarea name="reply_comment_details" id="reply_comment_details" cols="" rows=""></textarea>
                ชื่อ<input style="width:50%" name="reply_comment_name" id="reply_comment_name" type="text" /> 
                <input name="reply_submit" type="button" value="ตอบกระทู้" data-theme="b" data-icon="star" data-iconpos="right" onclick="addReplyComment();" />
            </form>
            <hr />
        </div>
        <div style="clear:both;">&nbsp;</div> 
        <div id="dv_reply">
            <?php
            $sql_reply = $db->select_query('SELECT reply_id, activity_id, reply_details, cr_by, cr_date, cr_ip FROM activity_reply WHERE activity_id =' . $db->antihack($_GET['activityId']) . ' ORDER BY reply_id ASC LIMIT 20 ');
            $i = 0;
            while($reply = $db->fetch($sql_reply))
            {
                ?>
                <div class="countReply" data-ctype='reply_activity' data-activityid='<?php echo $reply['activity_id']; ?>' data-replyid='<?php echo $reply['reply_id']; ?>' data-numreply='<?php echo++$i; ?>'>
                    <div class="reply_status" id="reply_status" >
                        <div id="pic_user" class="pic_user"><img src="images/pic_user.png" width="45" height="45" /></div>
                        <div id="count_comment" style="float:right">ความคิดเห็นที่&nbsp;&nbsp;<?php echo $i; ?> </div>
                        <span class="cr_by"><?php echo $reply['cr_by'] . '<br>'; ?> </span>
                        <?php
                        $temp = explode(' ', $reply['cr_date']);

                        $tempDate = explode('-', $temp['0']);
                        $day = $tempDate['2'];
                        $month = $tempDate['1'];
                        $year = $tempDate['0'];
                        $time = $temp['1'];
                        $date_time = $day . ' ' . getMonthName($month) . ' ' . ($year + 543) . ' - ' . $time;
                        ?>
                        <span class="cr_date"><?php echo '<strong>เมื่อ : </strong>' . $date_time . '<br><strong>&nbsp;&nbsp;&nbsp;ไอพี : </strong>' . $reply['cr_ip']; ?> </span>
                    </div>
                    <div class="reply_details" id="reply_detail"> <br /><?php echo $reply['reply_details']; ?><br />
                        <div id="link_top_page" align="right">
                            <span class="cr_date">
                                <a href="#top"><img src="images/up_to_page_icon.png" width="16" height="16" />ขึ้นบนสุด</a> 
                            </span>
                        </div>
                    </div>
                    <div>&nbsp;</div>
                </div>
                <?php
            }
            ?>
        </div>
        <?php
        $numReply = $db->rows($sql_reply);
        if($numReply != 0)
        {
            ?>
            <div id="loadding-text" style="clear:both;">&nbsp;&nbsp;</div>
            <div id="loading" align="center"></div>
            <?php
        }
        ?>
    </div><!-- /content -->
    <div data-role="footer" >
        <h4>Version 1.0 Beta</h4>
        <a id="link_web" href="http://www.mocyc.com" data-icon="refresh" data-iconpos="right" class="ui-btn-right">แบบปกติ</a>

    </div><!-- /footer -->
</div><!-- /page -->